package com.hengpeng.itfinoss.service.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.entity.OperateLog;
import com.hengpeng.common.persist.entity.OperateLog.OperationType;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinoss.service.OperateLogService;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * <pre>
 * 操作日志服务层实现
 * </pre>
 * @author CHARLES
 * @version 1.0, 2010-8-10
 */
@Service
@Lazy
public class OperateLogServiceImpl implements OperateLogService
{

	@Autowired
	private ServiceFactory serviceFactory;

	/**
	 * <pre>
	 * 保存日志信息
	 * </pre>
	 * @param userId 操作员编号
	 * @param entityName 操作实体名称
	 * @param entityId 操作实体主键
	 * @param operateTime 创建时间
	 * @param description 描述
	 */
	public void operateLogSave(String userId, String entityName, String entityId, Date operateTime, String description)
	{
		OperateLog log = new OperateLog();
		log.setOperationType(OperationType.CREATE);
		log.setUserId(userId);
		log.setEntityName(entityName);
		log.setEntityId(entityId);
		log.setOperateTime(operateTime);
		log.setDescription(description);
		serviceFactory.getPersistService().save(log);
	}

	/**
	 * <pre>
	 * 更新数据 操作
	 * </pre>
	 * @param userId 操作员编号
	 * @param entityName 操作实体名称
	 * @param entityId 操作实体主键
	 * @param operateTime 创建时间
	 * @param description 描述
	 */
	public void operateLogModify(String userId, String entityName, String entityId, Date operateTime, String description)
	{
		OperateLog log = new OperateLog();
		log.setOperationType(OperationType.UPDATE);
		log.setUserId(userId);
		log.setEntityName(entityName);
		log.setEntityId(entityId);
		log.setOperateTime(operateTime);
		log.setDescription(description);
		serviceFactory.getPersistService().save(log);
	}

	/**
	 * <pre>
	 * 删除数据 操作
	 * </pre>
	 * @param userId 操作员编号
	 * @param entityName 操作实体名称
	 * @param entityId 操作实体主键
	 * @param operateTime 创建时间
	 * @param description 描述
	 */
	public void operateLogDelete(String userId, String entityName, String entityId, Date operateTime, String description)
	{
		OperateLog log = new OperateLog();
		log.setOperationType(OperationType.DELETE);
		log.setUserId(userId);
		log.setEntityName(entityName);
		log.setEntityId(entityId);
		log.setOperateTime(operateTime);
		log.setDescription(description);
		serviceFactory.getPersistService().save(log);
	}

	@Override
	public ServiceResult<Long> selectOperateLogCount(String userId, String entityName, String entityId,
			Date operateTime1, Date operateTime2, String description, OperationType operationType)
	{
		DynamicQuery pagination = new DynamicQuery(OperateLog.class);
		if (userId != null && userId.trim().length() > 0)
			pagination.like("userId", "%" + userId + "%");
		if (entityName != null && entityName.trim().length() > 0)
			pagination.eq("entityName", entityName);
		if (entityId != null && entityId.trim().length() > 0)
			pagination.eq("entityId", entityId);
		if (description != null && description.trim().length() > 0)
			pagination.like("description", "%" + description + "%");
		if (operationType != null && operationType.toString().trim().length() > 0)
			pagination.eq("operationType", operationType);
		if (operateTime1 != null && operateTime1.toString().trim().length() > 0 && operateTime2 != null
				&& operateTime2.toString().trim().length() > 0)
			pagination.between("operateTime", operateTime1, operateTime2);
		return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(pagination,null));
	}

	@Override
	public ServiceResult<List<OperateLog>> selectOperateLogPage(String userId, String entityName, String entityId,
			Date operateTime1, Date operateTime2, String description, OperationType operationType, int pageIndex,
			int pageSize, LinkedHashMap<String, OrderType> sortMap)
	{
		DynamicQuery pagination = new DynamicQuery(OperateLog.class);
		if (userId != null && userId.trim().length() > 0)
			pagination.like("userId", "%" + userId + "%");
		if (entityName != null && entityName.trim().length() > 0)
			pagination.like("entityName", "%" + entityName + "%");
		if (entityId != null && entityId.trim().length() > 0)
			pagination.eq("entityId", entityId);
		if (description != null && description.trim().length() > 0)
			pagination.like("description", "%" + description + "%");
		if (operationType != null && operationType.toString().trim().length() > 0)
			pagination.eq("operationType", operationType);
		if (operateTime1 != null && operateTime1.toString().trim().length() > 0 && operateTime2 != null
				&& operateTime2.toString().trim().length() > 0)
			pagination.between("operateTime", operateTime1, operateTime2);
		pagination.setPageIndex(pageIndex);
		pagination.setPageSize(pageSize);
		return new ServiceResult<List<OperateLog>>(serviceFactory.getPersistService()
				.find(OperateLog.class, pagination));
	}
}
